﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NativeDataOperation.Interfaces;
using DDHDeepTunnel.EquipmentManagement.Data;
using DDHDeepTunnel.EquipmentManagement.Interfaces;
using WebFoundation;
using WebFoundation.Data;
using log4net.Core;

namespace DDHDeepTunnel.EquipmentManagement.Business
{
    internal class OnlineDataModule : WFModule, IOnlineDataModule
    {
        private readonly DbManager _dbQueryManager;
        private readonly DbManager _dbWriteManager;
        
        private readonly SystemLogQueryDAO _systemLogQueryDAO;
        private readonly SystemLogWritingDAO _systemLogWritingDAO;
        public OnlineDataModule(string queryConnectionString, string writableConnectionString)
        {
            _dbQueryManager = DbManager.FromConfig(queryConnectionString);
            _dbWriteManager = DbManager.FromConfig(writableConnectionString);

            _systemLogQueryDAO = new SystemLogQueryDAO(_dbQueryManager);
            _systemLogWritingDAO = new SystemLogWritingDAO(_dbWriteManager);
            BatchInfos = new Batch(_dbQueryManager, _dbWriteManager, this);
            ProcedureInfos = new Procedure(_dbQueryManager, _dbWriteManager, this);
            TableInfos = new Table(_dbQueryManager, _dbWriteManager, this);
            SystemLogInfos = new SystemLogInfo(_systemLogQueryDAO, _systemLogWritingDAO, this);
        }
        
        public IBatch BatchInfos { get; private set; }

        public IProcedure ProcedureInfos { get; private set; }

        public ITable TableInfos { get; private set; }

        public ISystemLogInfo SystemLogInfos { get; private set; }

        public IEnumerable<SystemLog> GetSystemLogInfoByLogType(string logType, DateTime startTime,
            DateTime endTime)
        {
            return this.SystemLogInfos.GetSystemLogInfoByLogType(logType, startTime,
                endTime);
        }

        public IEnumerable<SystemLog> GetSystemLogInfoByLogTypes(string logTypes, DateTime startTime,
            DateTime endTime)
        {
            return this.SystemLogInfos.GetSystemLogInfoByLogTypes(logTypes, startTime,
                endTime);
        }

        public bool AddSystemLogInfo(SystemLog systemLog)
        {
            return this.SystemLogInfos.AddSystemLogInfo(systemLog);
        }

        protected override void Dispose(bool disposing)
        {
            if (!this.disposed && disposing)
            {
                _dbQueryManager.Dispose();
                _dbWriteManager.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}
